package com.booklove.controller.delivery;

import com.booklove.domain.dto.PageDTO;
import com.booklove.domain.query.DeliverQuery;
import com.booklove.domain.vo.DeliverVO;
import com.booklove.result.Result;
import com.booklove.service.delivery.DeliverService;
import com.booklove.domain.po.Deliver;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;



@Slf4j
@RestController("deliverCLL")
@RequestMapping("/deliver")
@RequiredArgsConstructor// 使用基本注解的注入形式，仅仅注入需要的变量
public class DeliverController {

    private final DeliverService deliverService;

    /**
     * 保存新增
     */
//    @PostMapping("/")
//    //@AutoFill(OperationType.INSERT)
//    Result<Deliver> saveDeliver(@RequestBody DeliverFormDTO deliverFormDTO) {
//        Deliver deliver = new Deliver();
//        BeanUtils.copyProperties(deliverFormDTO, deliver);
//        //默认状态为未派送
//        deliver.setDeliverState(StatusConstant.NOT_DELIVER);
//
//        deliver.setCreatedTime(LocalDateTime.now());
//        deliver.setUpdatedTime(LocalDateTime.now());
//        deliver.setCreatedBy(BaseContext.getCurrentId().intValue());
//        deliver.setUpdatedBy(BaseContext.getCurrentId().intValue());
//        deliverService.save(deliver);
//        return Result.success(deliver);
//    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/id")
    Result<String> deleteDeliver(@RequestParam("id") Long id) {
        deliverService.removeById(id);
        return Result.success();
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    DeliverVO getDeliver(@PathVariable Long id) {
        Deliver deliver = deliverService.getById(id);
        DeliverVO deliverVO=new DeliverVO();

        BeanUtils.copyProperties(deliver,deliverVO);
        return deliverVO;
    }

    /**
     * 分页查询
     *
     * @param deliverQuery 筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    PageDTO<DeliverVO> deliversPageQuery(DeliverQuery deliverQuery) {
        return deliverService.queryDeliversPage(deliverQuery);
    }
}

